/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.unifei.VRaptor.dao;

import br.com.caelum.vraptor.ioc.Component;
import br.edu.unifei.VRaptor.modelo.Mentor;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Douglas Soares
 */
@Component
public class MentorDao {

    private EntityManager em;

    public MentorDao(EntityManager em) {
        this.em = em;
    }

    public void create(Mentor mentor) {
        em.getTransaction().begin();
        em.persist(mentor);
        em.getTransaction().commit();
    }

    public void update(Mentor mentor) {
        em.getTransaction().begin();
        em.merge(mentor);
        em.getTransaction().commit();
    }

    public void delete(String cpf) {
        em.getTransaction().begin();
        Mentor c = findMentor(cpf);
        em.remove(c);
        em.getTransaction().commit();
    }

    public Mentor findMentor(String cpf) {
        return (Mentor) em.createQuery(
                "from Mentor where cpf = '" + cpf + "'").getSingleResult();
    }

    public List<Mentor> findAllMentor() {
        return em.createQuery("from Mentor").getResultList();
    }

    public List<Mentor> findPageMentor(int max, int first) {
        Query query = em.createQuery("from Mentor");
        query.setMaxResults(max);
        query.setFirstResult(first);
        return query.getResultList();
    }

    public long getCountMentor() {
        Query query = em.createQuery("from Mentor");
        return (Long) em.createQuery(
                "select count(*) from Mentor").getSingleResult();
    }
}
